package entities;

import com.haxepunk.Entity;
import com.haxepunk.graphics.Spritemap;
import com.haxepunk.tmx.TmxObject;

enum StateMagnet{
	IDLE; ACTIVATION; ACTIVE; DEACTIVATION;
}

/**
 * ...
 * @author ... Marcelo Ruben Guardia
 */
class Magnet extends Entity
{
	private var spMagnet:Spritemap;
	private var countImpulses:Int;
	private var state:StateMagnet;
	private var refProtobot:Protobot;

	public function new(obj:TmxObject) 
	{
		super(obj.x, obj.y);
		
		state = IDLE;
		spMagnet = new Spritemap(GC.IMG_magnet, 64, 135);
		spMagnet.add(IDLE.getName(), [0], 0, false);
		spMagnet.add(ACTIVATION.getName(), [0, 1, 2, 3, 4, 5, 6], 8, false);
		spMagnet.add(ACTIVE.getName(), [6], 0, false);
		spMagnet.add(DEACTIVATION.getName(), [6, 5, 4, 3, 2, 1, 0], 8, false);
		spMagnet.play(state.getName());
		spMagnet.scale = 0.5;
		spMagnet.originX = 32;
		spMagnet.originY = 122;
		
		setHitbox(15, 60, 8, 60);
		graphic = spMagnet;
		type = "magnet";
		layer = -1;
		refProtobot = null;
	}
	
	override public function update():Void
	{
		super.update();
		switch(state)
		{
			case IDLE:
				setHitbox(2, 2, 1, 1);
				
			case ACTIVATION:
				setHitbox(2, 2, 1, 1);
				if (spMagnet.complete)
					ChangeState(ACTIVE);
			
			case ACTIVE:
				//setHitbox(64, 50, 0, 0);
				setHitbox(15, 60, 8, 60);
			
			case DEACTIVATION:
				setHitbox(2, 2, 1, 1);
				
				if (spMagnet.complete)
					ChangeState(IDLE);
		}
		
		//if (collide("", x, y) != null)
		//{
			//
		//}
		//
		//if (collide("", x, y) != null)
		//{
			//
		//}
 }
	
	public function ChangeState(newState:StateMagnet):Void
	{
		state = newState;
		spMagnet.play(state.getName());
	}
	
	public function ChangeMagnet():Void
	{
		if (state == IDLE)
			ChangeState(ACTIVATION);
		else
		if (state == ACTIVE)
			ChangeState(DEACTIVATION);
	}
	
	public function GetState():StateMagnet
	{
		return state;
	}
	
	public function SetProtobot(proto:Protobot):Void
	{
		refProtobot = proto;
	}
	
}